<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="renderer" content="webkit">
    <title><?php echo ($title); ?></title>
    <link rel="stylesheet" href="/Public/web/css/pintuer.css">
    <link rel="stylesheet" href="/Public/web/css/admin.css">
    <script src="/Public/web/js/jquery.js"></script>
    <script src="/Public/web/js/pintuer.js"></script>
</head>

<script type="text/javascript" src="/Public/js/jquery-latest.js"></script>
<script type="text/javascript" src="/Public/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="/Public/ueditor/1.4.3/ueditor.all.min.js"> </script>

<link rel="stylesheet" href="/Public/zyUpload/css/zyUpload.css" type="text/css">
<script type="text/javascript" src="/Public/zyUpload/js/zyFile.js"></script>
<script type="text/javascript" src="/Public/zyUpload/js/zyUpload.js"></script>

<body>
  <script>
    $(function(){
      $("#zyUpload").zyUpload({
        width:"650px",height:"",itemWidth:"120px",itemHeight:"100px",
        url:"<?php echo U('Goods/zyUpdate');?>",multiple:true,
        dragDrop:true,del:true,finishDel:false
      });
    });
    var ue = UE.getEditor('editor');

    /*******************/
    var step = {
      Creat_Table: function() {
        step.hebingFunction();
        var SKUObj = $('.Father_Title');
        var arrayTile = new Array();
        var arrayInfor = new Array();
        var arrayColumn = new Array();
        var bCheck = true;
        var columnIndex = 0;

        $.each(SKUObj, function(i, item) {
          arrayColumn.push(columnIndex++);
          arrayTile.push(SKUObj[i].value);
          var itemName = $('.check_item')[i].value.split("/");
          var bCheck2 = true;

          var order = new Array();
          $.each(itemName,function(y, item) {
            order.push(itemName[y]);
          });

          arrayInfor.push(order);
          if (order.join() == '') {
            bCheck = false;
          }
        })
        if (bCheck) {
          $('#createTable').html('');
          var table = $('<table id="process" class="columnList"></table>');
          table.appendTo($('#createTable'));
          var thead = $('<thead></thead>');
          thead.appendTo(table);
          var trHead = $('<tr></tr>');
          trHead.appendTo(thead);
          var str = '';
          $.each(arrayTile, function(index, item) {
            str += '<th width="100">' + item + '</th>';
          })
          str += '<th width="200">销售价</th><th width="200">市场价</th><th width="200">库存</th><th width="50">默认</th>';
          trHead.append(str);
          var tbody = $('<tbody></tbody>');
          tbody.appendTo(table);

          var zuheDate = step.doExchange(arrayInfor);
          if (zuheDate.length > 0) {
            $.each(zuheDate, function(index, item) {
              var td_array = item.split(',');
              var tr = $('<tr></tr>');
              tr.appendTo(tbody);
              var str = '';
              var temp = '';
              $.each(td_array, function(i, values) {
                str += '<td>' + values + '</td>';
                temp += values + '/';
              });
              str += '<td><input name="it_reprice[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="it_maprice[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="it_stock[]" class="inpbox inpbox-mini" value="0" type="text"></td>';
              str += '<td><input name="checked" value="'+index+'" type="radio">';
              str += '<input name="it_name[]" value="'+temp+'" type="hidden"></td>';
              tr.append(str);
            });
          }
          arrayColumn.pop();
          $(table).mergeCell({
            cols: arrayColumn
          });
        } else {
          document.getElementById('createTable').innerHTML = "";
        }
      },
      hebingFunction: function() {
        $.fn.mergeCell = function(options) {
          return this.each(function() {
            var cols = options.cols;
            for (var i = cols.length - 1; cols[i] != undefined; i--) {
              mergeCell($(this), cols[i]);
            }
            dispose($(this));
          })
        };

        function mergeCell($table, colIndex) {
          $table.data('col-content', '');
          $table.data('col-rowspan', 1);
          $table.data('col-td', $());
          $table.data('trNum', $('tbody tr', $table).length);
          $('tbody tr', $table).each(function(index) {
            var $td = $('td:eq(' + colIndex + ')', this);
            var currentContent = $td.html();
            if ($table.data('col-content') == '') {
              $table.data('col-content', currentContent);
              $table.data('col-td', $td);
            } else {
              if ($table.data('col-content') == currentContent) {
                var rowspan = $table.data('col-rowspan') + 1;
                $table.data('col-rowspan', rowspan);
                $td.hide();
                if (++index == $table.data('trNum'))
                  $table.data('col-td').attr('rowspan', $table.data('col-rowspan'));
              }
              else {
                if ($table.data('col-rowspan') != 1) {
                  $table.data('col-td').attr('rowspan', $table.data('col-rowspan'));
                }
                $table.data('col-td', $td);
                $table.data('col-content', $td.html());
                $table.data('col-rowspan', 1);
              }
            }
          })
        }
        function dispose($table) {
          $table.removeData();
        }
      },
      doExchange: function(doubleArrays) {
        var len = doubleArrays.length;
        if (len >= 2) {
          var arr1 = doubleArrays[0];
          var arr2 = doubleArrays[1];
          var len1 = arr1.length;
          var len2 = arr2.length;
          var newLen = len1 * len2;
          var temp = new Array(newLen);
          var index = 0;
          for (var i = 0; i < len1; i++) {
            for (var j = 0; j < len2; j++) {
              temp[index++] = arr1[i] + ',' + arr2[j];
            }
          }
          var newArray = new Array(len - 1);
          newArray[0] = temp;
          if (len > 2) {
            var _count = 1;
            for (var i = 2; i < len; i++) {
              newArray[_count++] = doubleArrays[i];
            }
          }
          return step.doExchange(newArray);
        } else {
          return doubleArrays[0];
        }
      }
    }
    /*******************/
  </script>

  <div class="panel admin-panel">
    <div class="body-content">
      <form method="post" class="form-x" action="" enctype="multipart/form-data">

        <div class="form-group">
          <div class="label">
            <label><?php echo L("shangpinmingcheng");?>：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" value="<?php echo ($info['goods_name']); ?>" name="goods_name" data-validate="required:<?php echo L('qingshuru'); echo L('shangpinmingcheng');?>" />
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("shangpinfenlei");?>：</label>
          </div>
          <div class="field">
            <select name="goods_cate_id" class="input w50">
              <?php if(empty($info['goods_cate_id'])): ?><option value="0"><?php echo L("qingxuanze");?></option><?php endif; ?>
              <?php if(is_array($cate_list)): foreach($cate_list as $key=>$v): ?><option value="<?php echo ($v['goods_cate_id']); ?>" <?php if($v['goods_cate_id'] == $info['goods_cate_id']): ?>selected="selected"<?php endif; ?>><?php echo ($v['goods_cate_name']); ?></option>
                <?php if($v['sonlist']): if(is_array($v['sonlist'])): foreach($v['sonlist'] as $key=>$v2): ?><option value="<?php echo ($v2['goods_cate_id']); ?>" <?php if($v2['goods_cate_id'] == $info['goods_cate_id']): ?>selected="selected"<?php endif; ?>>---<?php echo ($v2['goods_cate_name']); ?></option><?php endforeach; endif; endif; endforeach; endif; ?>
            </select>
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("suolvetu");?>：</label>
          </div>
          <div class="field">
            <input type="file" class="button bg-blue" name="default_img" value="+ <?php echo L('linlanshangchuan');?>" onchange="preview(this)" style="float: left;">
            <div id="preview" name="img" style="float: left;width: 10%;box-sizing: border-box;" class="input">
              <?php if(empty($info['default_img'])): ?><img src="/Public/images/zanwu.jpg" width="100%" alt=""/>
                <?php else: ?>
                <img src="/<?php echo ($info['default_img']); ?>" width="100%" alt=""/><?php endif; ?>
            </div>
            <div class="tipss"></div>
          </div>
        </div>

        <div class="form-group" style="margin-bottom: 0; padding-bottom: 0">
          <div class="label">
            <label><?php echo L("xiangqingtupian");?>：</label>
          </div>
          <div class="field">
            <div id="zyUpload" class="demo"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label"><?php echo L("dianjishanchu");?></div>
          <div class="field" id="myimg">
            <style>
              #myimg {width: 650px}
              #myimg img{border: #ccc 3px solid; margin: 4px; cursor: pointer; height: 120px;}
              #myimg img:hover{border: #f00 3px solid;}
            </style>
            <?php if(is_array($img_list)): foreach($img_list as $key=>$v): ?><img src="<?php echo ($v['goods_imgurl']); ?>" class="img<?php echo ($v['goods_img_id']); ?>" onclick="return del(<?php echo ($v['goods_img_id']); ?>,<?php echo ($v['goods_id']); ?>,this)"><?php endforeach; endif; ?>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("xiaoshoujia");?>：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="price" value="<?php echo ($info['price']); ?>" data-validate="required:<?php echo L('qingshuru'); echo L('xiaoshoujia');?>"/>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label><?php echo L("shichangjia");?>：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="market_price" value="<?php echo ($info['market_price']); ?>"/>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label><?php echo L("kucun");?>：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="stock" value="<?php echo ($info['stock']); ?>" data-validate="number:<?php echo L('hefakucun');?>" />
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("shangpinshuxing");?>：</label>
          </div>
          <div class="field">
            <select name="goods_type" class="input w50">
              <option value="0"><?php echo L("weikaiqi");?></option>
              <option value="1" <?php if($info['goods_type'] == 1): ?>selected="selected"<?php endif; ?>><?php echo L("kaiqi");?></option>
            </select>
            <div class="tipss"></div>
          </div>
        </div>

        <style>
          #mytype input{ float: none; display: inline; }
          .columnList {border-collapse: collapse;}
          .columnList th {background-color: #eee;}
          .columnList th,.columnList td {padding: 5px 10px;border: 1px solid #ddd;text-align: center;vertical-align: middle;}
          .columnList input{padding: 3px;}
          #mytype{background: #f0f0f0; padding: 10px 0;}
          #mytypeitem{padding: 10px 0;}
        </style>
        <div id="mytype" <?php if($info['goods_type'] != 1): ?>style="display: none"<?php endif; ?>>
          <?php if($info['goods_type'] != 1): ?><div class="form-group">
              <div class="label"><label><?php echo L("guige");?>：</label></div>
              <div class="field">
                <input type="text" class="Father_Title input w50" name="spec_name[]" placeholder="<?php echo L('guigemingchen');?>"/>　
                <input type="text" class="input w50 check_item" name="item_name[]" placeholder="<?php echo L('guigeshuxing');?>"/>　
                <a href="javascript:void(0)" class="mya1 button bg-main"><?php echo L("tianjiaguige");?></a>　
                <a href="javascript:void(0)" class="button bg-main" onclick="refreshOptions();"><?php echo L("scguigebiao");?></a>
              </div>
            </div>
            <?php else: ?>
            <?php echo ($mytype); endif; ?>
        </div>

        <div id="mytypeitem" <?php if($info['goods_type'] != 1): ?>style="display: none"<?php endif; ?>>
          <div class="form-group">
            <div class="label"><label><?php echo L("guigebiao");?>：</label></div>
            <div class="field" id="createTable"><?php echo ($mytypeitem); ?></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("qitashuxing");?>：</label>
          </div>
          <div class="field" style="padding-top:8px;"> 
            <?php echo L("xianshi");?> <input name="is_show" type="checkbox" value="1" <?php if($info['is_show'] == 1): ?>checked="checked"<?php endif; ?>/>
            <?php echo L("zhuye");?> <input name="is_home" type="checkbox" value="1" <?php if($info['is_home'] == 1): ?>checked="checked"<?php endif; ?>/>
            <?php echo L("remen");?> <input name="is_hot" type="checkbox" value="1" <?php if($info['is_hot'] == 1): ?>checked="checked"<?php endif; ?>/>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label><?php echo L("shangpinjianjie");?>：</label>
          </div>
          <div class="field">
            <textarea class="input w50" name="goods_introduce" style="width: 650px"><?php echo ($info['goods_introduce']); ?></textarea>
            <div class="tips"></div>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label><?php echo L("paixu");?>：</label>
          </div>
          <div class="field">
            <input type="text" class="input w50" name="sort" value="<?php echo ($info['sort']); ?>" data-validate="number:<?php echo L('hefapaixu');?>" />
            <div class="tips"></div>
          </div>
        </div>

        <div class="form-group">
          <div class="label">
            <label><?php echo L("xiangqing");?>：</label>
          </div>
          <div class="field" style="padding-top:8px;">
            <script id="editor" type="text/plain" style="width:90%;height:400px;" name="goods_describe"><?php echo ($info['goods_describe']); ?></script>
          </div>
        </div>
        <div class="form-group">
          <div class="label">
            <label></label>
          </div>
          <div class="field">
            <button class="button bg-main icon-check-square-o" type="submit"> <?php echo L("tijiao");?></button>
          </div>
        </div>

      </form>
    </div>
  </div>
  <script type="text/javascript">
    $(document).ready(function(){
      $("select[name='goods_type']").change(function(){
        var typeon = $("select[name='goods_type']").val();
        if(typeon==1){$("#mytype").show();}
        else{$("#mytype").hide();$("#mytypeitem").hide();}
      });

      $(".mya1").click(function(){
        var html = '<div class="form-group"> ';
        html += '<div class="label"><label></label></div>';
        html += '<div class="field"> ';
        html += '<input type="text" class="Father_Title input w50" name="spec_name[]" placeholder="<?php echo L("guigemingchen");?>" data-validate="required: " />　';
        html += ' <input type="text" class="input w50 check_item" name="item_name[]" placeholder="<?php echo L("guigeshuxing");?>" data-validate="required: " />　';
        html += ' <a href="javascript:void(0)" class="mya1 button bg-red" onclick="itemdel(this)"><?php echo L("shanchuguige");?></a>';
        html += '</div></div>';

        $("#mytype").append(html);
      });
    });

    function preview(file)
    {
      var prevDiv = document.getElementById('preview');
      if (file.files && file.files[0])
      {
        var reader = new FileReader();
        reader.onload = function(evt){
          prevDiv.innerHTML = '<img src="' + evt.target.result + '" width="100%" />';
        }
        reader.readAsDataURL(file.files[0]);
      }
      else
      {
        prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
      }
    }
    //单个删除
    function del(id,goods_id,t){
      if(confirm("<?php echo L('shanchuxiaoxi');?>")){
        var url = "<?php echo U('goods/delpic');?>";
        $.getJSON(url,{id:id,goods_id:goods_id},function (data){
          if(data.result=="true1"){$(t).remove();}
          else if(data.result=="true2"){$(t).next('input[type=hidden]').remove();$(t).remove();}
        })
      }
    }
    function itemdel(t){
      $(t).parents('.form-group').remove();
    }
    //生成表格
    function refreshOptions(){
      $("#mytypeitem").show();
      step.Creat_Table();
    }
  </script>
</body>
</html>